package com.lenovo.manufacture.Fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.lenovo.manufacture.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;

import static androidx.constraintlayout.motion.widget.MotionScene.TAG;

public class BlankFragment_six extends Fragment {
    @BindView(R.id.tv_ye)
    TextView tvYe;
    @BindView(R.id.ed_carno)
    EditText edCarno;
    @BindView(R.id.ed_czje)
    EditText edCzje;
    @BindView(R.id.bu_congzhi)
    Button buCongzhi;
    private List<Six_list> six_lists = new ArrayList<>();
    public BlankFragment_six() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_six, container, false);
        ButterKnife.bind(this, view);
        return view;
    }
    @OnClick(R.id.bu_congzhi)
    public void onViewClicked() {
        initData();
    }
    public interface BlogService {
        @GET("carlist")
        Call<ResponseBody> getBlog();
    }
    public interface BlogServicea {
        @POST("charge")
        Call<ResponseBody> getBloga(@Body RequestBody user);
    }
    private void initData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        Map<String, String> map = new HashMap<>();
        map.put("who", "张三");
        if (edCarno.getText().toString().equals("辽A10001")){
            map.put("carno", edCarno.getText().toString().trim());
            Toast.makeText(getContext(), "充值成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getContext(), "只有辽A10001给你充", Toast.LENGTH_SHORT).show();
        }
        map.put("amount", edCzje.getText().toString().trim());
        Gson gson = new Gson();
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), gson.toJson(map));
        BlogServicea servicea = retrofit.create(BlogServicea.class);
        Call<ResponseBody> call = servicea.getBloga(requestBody);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                initview();
                six_lists = new ArrayList<>();
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onResponseada"+12345);
                Toast.makeText(getContext(), "充值失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initview() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        BlogService service = retrofit.create(BlogService.class);
        Call<ResponseBody> call = service.getBlog();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Gson gson = new Gson();
                try {
                    String s = response.body().string();
                    Log.i(TAG, "onFailureda" + s);
                    Six_list carlist = gson.fromJson(s, Six_list.class);
                    six_lists.add(carlist);
                    tvYe.setText("车辆余额:" + six_lists.get(0).getData().get(0).getAmount());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailureda" + 123);
            }
        });
    }
}